<template>
  <el-dialog :title="title" :visible="showDialog" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <el-form-item label="姓名:" prop="username">
        <el-input
          v-model="form.username"
          style="width: 50%"
          placeholder="1-30个字符"
        />
      </el-form-item>
      <el-form-item label="手机:" prop="mobile">
        <el-input
          v-model="form.mobile"
          style="width: 40%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间:" prop="timeOfEntry">
        <div class="block">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
            value-format="timestamp"
          />
        </div>
      </el-form-item>
      <el-form-item label="聘用形式:" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号:" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          style="width: 50%"
          placeholder="1-20个字符"
        />
      </el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <el-tree
          v-if="showTree"
          v-Clickoutside="close"
          :data="treeData"
          :props="defaultProps"
          :default-expand-all="true"
          icon-class=""
          :v-loading="loading"
          class="location"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间:" prop="correctionTime">
        <div class="block">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
            value-format="timestamp"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'
import Clickoutside from 'element-ui/src/utils/clickoutside'
export default {
  directives: { Clickoutside },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '姓名长度为1-30', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
          {
            pattern: /^(?:(?:\+|00)86)?1\d{10}$/,
            message: '手机格式错误',
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '工号长度为1-20', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' }
        ]
      },
      date: '',
      options: [
        {
          value: '1',
          label: '正式'
        },
        {
          value: '2',
          label: '非正式'
        }
      ],
      value: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      loading: true,
      showTree: false
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          await addEmployee(this.form)
          this.$emit('update:showDialog', false)
          // console.log(1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      // console.log(this.treeData)
      this.showTree = true
    },
    selectNode(node) {
      this.form.departmentName = node.name
      this.$refs.form.clearValidate()
      this.showTree = false
    },
    cancel() {
      this.form = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    },
    close() {
      this.showTree = false
    }
  }
}
</script>

<style scoped>
.location {
  position: absolute;
  top: 40px;
  left: 0;
  z-index: 9999;
  width: 50%;
}
</style>
